package com.tz.cartelo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tz.cartelo.R;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.util.CommonUtils;

import java.util.List;


/**
 * Created by ok on 2016/9/18.
 */
public class ManagerStockAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private List<GoodsList> mGoodsListList;
    private Context mContext;
    private int mImgWidth, mImgHeight;

    public ManagerStockAdapter(Context context, List<GoodsList> goodsListList) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mGoodsListList = goodsListList;
        setImgSize();
    }

    private void setImgSize() {
        mImgWidth = CommonUtils.getScreenWidth(mContext) / 3;
        mImgHeight = mImgWidth;
        //+ mContext.getResources().getDimensionPixelOffset(R.dimen.margin_5)
    }

    @Override
    public int getCount() {
        return mGoodsListList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.item_manager_stock, null);
            holder = new ViewHolder();
            holder.itemGoodsCenterImg = (ImageView) view.findViewById(R.id.item_goods_center_img);
            holder.itemManagerStockNameText = (TextView) view.findViewById(R.id.item_manager_stock_name_text);
            holder.itemManagerStockPriceText = (TextView) view.findViewById(R.id.item_manager_stock_price_text);
            holder.itemManagerStockSaleNumText = (TextView) view.findViewById(R.id.item_manager_stock_sale_num_text);
            holder.itemManagerStockStockNumText = (TextView) view.findViewById(R.id.item_manager_stock_stock_num_text);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(mImgWidth, mImgHeight);
        holder.itemGoodsCenterImg.setLayoutParams(layoutParams);

//        ImageLoader.getInstance().displayImage("drawable://" + R.mipmap.ic_holder_list, holder.itemGoodsDetailsImg, CommonUtils.optionsList());
        ImageLoader.getInstance().displayImage(mGoodsListList.get(i).getGoodsPicture(), holder.itemGoodsCenterImg, CommonUtils.optionsList());
        holder.itemManagerStockNameText.setText(mGoodsListList.get(i).getTitle());
        holder.itemManagerStockPriceText.setText("¥" +mGoodsListList.get(i).getPrice());
        holder.itemManagerStockSaleNumText.setText(mGoodsListList.get(i).getSalesNum() + mContext.getResources().getString(R.string.goods_center_total_stock_unit));
        holder.itemManagerStockStockNumText.setText(mGoodsListList.get(i).getTotalNum()
                + mContext.getResources().getString(R.string.goods_center_total_stock_unit));
        return view;
    }

    private class ViewHolder {
        ImageView itemGoodsCenterImg;
        TextView itemManagerStockNameText;
        TextView itemManagerStockPriceText;
        TextView itemManagerStockSaleNumText;
        TextView itemManagerStockStockNumText;
    }

}
